package com.compass.mobile.presenters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.leanback.widget.Presenter
import com.compass.mobile.R
import com.compass.mobile.framework.ext.gone
import com.compass.mobile.framework.ext.visible
import com.compass.mobile.models.Doctor
import com.compass.mobile.models.Title
import dp2px
import setUrl
import setUrlRound

/**
 * Description： 问诊科室  医生 presenter
 * Date:2024/5/23 9:43
 * @param ItemType 1是预约 2是挂号
 */
class DoctorPresenter(private var itemType: String) : Presenter() {

    override fun onCreateViewHolder(viewGroup: ViewGroup): ViewHolder {
        val view = LayoutInflater.from(viewGroup.context)
            .inflate(R.layout.item_department_doctor, viewGroup, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(viewHolder: Presenter.ViewHolder, item: Any) {
        if (item is Doctor) {
            val vh = viewHolder as ViewHolder
            vh.tvTitle.text = item.name
            vh.tvPositionName.text = item.positionName
            vh.tvDepartmentName.text = item.departmentName
            vh.tvConsultationNum.text = "问诊数量：${getConsultationServiceNum(item)}"
            vh.tvGoodAt.text = item.meritIllness
            vh.tvConsultationCommentRate.text = "${getFavorableRate(item)}%"
            vh.imgHead.setUrlRound(item.avatar, dp2px(5f))
            //处方权
            if (item.prescriptionAllow == "1") vh.tvPrescription.visible() else vh.tvPrescription.gone()
            //检验检查权
            if (item.examinationAllow == "1") vh.tvInspect.visible() else vh.tvInspect.gone()
            //住院证权
            if (item.hospitalizedAllow == "1") vh.tvHospitalizationCertificate.visible() else vh.tvHospitalizationCertificate.gone()

            if (itemType == "1") {
                vh.llFollow.visible()
                vh.llConsultationRecord.visible()
            }
        }
    }
    fun getConsultationServiceNum(doctor: Doctor): String {
        doctor.consultationNum?.let {
            return it
        }?:kotlin.run {
            doctor. consultationServiceNum?.let {
                return it
            }
        }

        return "0"
    }

    private fun getFavorableRate(doctor:Doctor):String {
        doctor.favorableRate?.let {
            return it
        }?:kotlin.run {
            doctor.consultationCommentRate?.let {
                return it
            }
        }
        return ""
    }

    override fun onUnbindViewHolder(viewHolder: Presenter.ViewHolder) {

    }


    class ViewHolder(view: View) : Presenter.ViewHolder(view) {
        var tvTitle: TextView
        var tvPositionName: TextView
        var tvDepartmentName: TextView
        var tvPrescription: TextView
        var tvConsultationCommentRate: TextView
        var tvInspect: TextView
        var tvHospitalizationCertificate: TextView
        var tvGoodAt: TextView
        var tvConsultationNum: TextView
        var imgHead: ImageView
        var itemView: ConstraintLayout
        var llFollow: ConstraintLayout
        var llConsultationRecord: ConstraintLayout
        var layoutParams: ViewGroup.LayoutParams

        init {
            tvConsultationNum = view.findViewById(R.id.tvConsultationNum)
            tvConsultationCommentRate = view.findViewById(R.id.tvConsultationCommentRate)
            tvPositionName = view.findViewById(R.id.tvPositionName)
            tvDepartmentName = view.findViewById(R.id.tvDepartmentName)
            tvPrescription = view.findViewById(R.id.tvPrescription)
            tvInspect = view.findViewById(R.id.tvInspect)
            tvHospitalizationCertificate = view.findViewById(R.id.tvHospitalizationCertificate)
            tvGoodAt = view.findViewById(R.id.tvGoodAt)
            tvTitle = view.findViewById(R.id.tv_title)
            imgHead = view.findViewById(R.id.imgHead)
            llFollow = view.findViewById(R.id.llFollow)
            llConsultationRecord = view.findViewById(R.id.llConsultationRecord)
            itemView = view.findViewById(R.id.itemView)
            layoutParams = itemView.layoutParams
        }
    }
}
